package com.ailink.concurrent.thread;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Administrator on 2018/3/6
 */
public class MyAtomicTest2 implements Runnable {

    private AtomicInteger atomicInteger = new AtomicInteger(0);

    @Override
    public void run() {
        for(int j = 0; j < 100; j++) {
            atomicInteger.getAndIncrement();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Thread.yield();
        }
    }

    public static void main(String[] args) {
        MyAtomicTest2 myAtomicTest1 = new MyAtomicTest2();
//        Thread thread1 = new Thread(myAtomicTest1, "1");
//        Thread thread2 = new Thread(myAtomicTest1, "2");
//        Thread thread3 = new Thread(myAtomicTest1, "3");
//        thread1.start();
//        thread2.start();
//        thread3.start();
        for (int i = 0; i < 3; i++) {
            new Thread(myAtomicTest1, String.valueOf(i)).start();
        }
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(myAtomicTest1.atomicInteger);
        }
    }
}
